package cn.merge;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author summit
 * @since 2022/4/27 17:15
 */
public class PoiTest {

	public static void main(String[] args) throws FileNotFoundException, JsonProcessingException {
		String path = "C:\\Users\\summit\\Desktop\\wwww.xlsx";
		String path2 = "C:\\java-kaifa\\tongyu-workspace\\me\\springboot-jar\\excel\\src\\main\\resources\\1652167633.xlsx";
		POIDataReader reader = new POIDataReader(new FileInputStream(path));
		Map<Integer, List<String[]>> integerListMap = reader.readExcelContent(0);

		System.out.println(integerListMap);

		List<Map<String, String>> dataList = new ArrayList<>(integerListMap.get(0).size());
		Map<Integer, String> lastValue = new HashMap<>();
		for (int i = 0; i < integerListMap.get(0).size(); i++) {
			int index = 0;
			String[] data = integerListMap.get(0).get(i);
			Map<String, String> map = new HashMap<>();
			for (int j = 0; j < data.length; j++) {
				String currentVal = data[j];
				if ("".equals(currentVal) && index == 0) {
					currentVal = lastValue.get(j);
				} else {
					index = j;
				}
				lastValue.put(j, currentVal);
				map.put("a" + (j + 1), currentVal);
			}
			dataList.add(map);
		}

		System.out.println("end=========");

		ObjectMapper objectMapper = new ObjectMapper();
		System.out.println(objectMapper.writeValueAsString(dataList));

		String json = objectMapper.writeValueAsString(dataList);

		JsonNode jsonNode = objectMapper.readTree(json);
		List<List<String>> listData = new ArrayList<>();

		for (int i = 0; i < jsonNode.size(); i++) {
			List<String> strList = new ArrayList<>();
			JsonNode jn1 = jsonNode.get(i);
			Iterator<JsonNode> elements = jsonNode.get(i).elements();
			while (elements.hasNext()) {
				strList.add(elements.next().asText());
			}
			listData.add(strList);
		}

		System.out.println(listData);
		Map<String, List<List<String>>> map1 = new HashMap<>();
		map1.put("反洗钱问卷", listData.subList(1, listData.size()));
	//	ExcelCellMergeTest.createExcel(listData.get(0).toArray(new String[0]), map1, Arrays.asList( 0,1, 2, 3, 4 ));
	}
}
